package com.wenjh.jdmzt.android.view;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wenjh.jdmzt.AndroidKit;
import com.wenjh.jdmzt.protocol.URLProcessUtil;
import com.wenjh.jdmzt.util.StringUtils;
import com.wenjh.jdmzt.util.Toaster;
import com.wenjh.jdmzt.R;

public class MAlertWebviewDialog extends MAlertDialog {
	
	private WebView webView = null;
	private View loadingView = null;

	public MAlertWebviewDialog(Context paramContext) {
		super(paramContext);
		
		View view = AndroidKit.getLayoutInflater().inflate(R.layout.include_dialog_webview, null);
		setContentView(view);
		webView = (WebView) view.findViewById(R.id.webview);
		loadingView = view.findViewById(R.id.loading_indicator);
		
		initWebViews();
		setMinHeight(170);
	}
	
	public void setMinHeight(int dip) {
		findViewById(R.id.dialog_webview_layout_root).setMinimumHeight(AndroidKit.getPixels(dip));
	}
	
	/**
	 * 获取一个嵌入webview的对话框
	 * @param context
	 * @param url
	 * @param onClickListener 响应“确认”按钮的点击事件
	 * @return
	 */
	public static MAlertWebviewDialog makeConfimDialog(Context context, String url, OnClickListener onClickListener) {
		return makeConfimDialog(context, url, context.getString(R.string.confim), context.getString(R.string.cancle), onClickListener, null);
	}
	
	/**
	 * 只有一个按钮的对话框
	 * @param context
	 * @param url
	 * @param onClickListener
	 * @return
	 */
	public static MAlertWebviewDialog makeSingleDialog(Context context, String url, OnClickListener onClickListener) {
		MAlertWebviewDialog dialog = new MAlertWebviewDialog(context);
		dialog.setButton(0, context.getString(R.string.confim), onClickListener);
		
		if(!StringUtils.isEmpty(url)) {
			dialog.loadUrl(url);
		}
		
		return dialog;
	}
	
	public static MAlertWebviewDialog makeConfimDialog(Context context, String url,
			CharSequence btn1Text, CharSequence btn2Text,
			DialogInterface.OnClickListener btn1ClickListener,
			DialogInterface.OnClickListener btn2ClickListener) {
		MAlertWebviewDialog dialog = new MAlertWebviewDialog(context);
		dialog.setButton(0, btn1Text, btn1ClickListener).setBackgroundResource(R.drawable.btn_default_submit);   // 确认按钮使用不同的背景颜色
		dialog.setButton(1, btn2Text, btn2ClickListener);
		
		if(!StringUtils.isEmpty(url)) {
			dialog.loadUrl(url);
		}
		
		return dialog;
	}
	
	public WebView getWebView() {
		return webView;
	}
	
	public void loadUrl(String url) {
		webView.loadUrl(URLProcessUtil.appendParameter(url, "type", "dialog"));
	}
	
	private void initWebViews() {
		WebSettings webSettings= webView.getSettings();
		webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
			}
		});
		webView.setWebViewClient(new WebViewClient() {
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				if(errorCode == WebViewClient.ERROR_HOST_LOOKUP) {
					Toaster.show(R.string.errormsg_network_unfind);
				} else {
					Toaster.show(R.string.errormsg_server);
				}
				
				if(loadingView != null)
					loadingView.setVisibility(View.GONE);
			}
			
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				if(loadingView != null)
					loadingView.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				if(loadingView != null)
					loadingView.setVisibility(View.GONE);
			}
		});
	}
	
}
